package com.example.scaletest.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.scaletest.R;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by lenovo on 2017/5/19.
 */
public class LoginActivity extends Activity{

    private EditText accountEdit;
    private EditText passwordEdit;
    private Button login;
    public String LOING_API ="http://10.103.246.33:8080/data/signIn";
    private HttpURLConnection conn = null;
    private String finalResult = "";
    private String result="";
    public static String firm_id=null;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.login);
       accountEdit = (EditText) findViewById(R.id.account);
      passwordEdit= (EditText) findViewById(R.id.password);
      login=(Button) findViewById(R.id.login);
       login.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               String account =accountEdit.getText().toString();
               String password =passwordEdit.getText().toString();
               new Thread(new Runnable() {
                   @Override
                   public void run() {
                       try {
                          result= checkPassword(account,password,LOING_API );
                          JSONObject Object = new JSONObject(result.toString());
                        if(Object.getBoolean("result")) {
                           // postToUIThread("登录成功");
                            firm_id=Object.getString("firm_id");
                            Intent intent=new Intent(LoginActivity.this,MenuActivity.class);
                            startActivity(intent);
                        }
                        else {
                            postToUIThread("登录失败");
                        }
                       } catch (JSONException e) {
                           e.printStackTrace();
                       }


                   }
               }).start();
               //int flag= checkPassword(account, password);
               //intent = new Intent(LoginActivity.this, NetWorkService.class);

           }
       });
    }

    public String checkPassword (String acc, String password, String url) throws JSONException {
        //Intent intent = new Intent(LoginActivity.this, NetWorkService.class);
      //  intent.putExtra("account",account);
      //  intent.putExtra("account",account);
        BufferedReader reader;
        OutputStreamWriter writer;

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("account", acc);
        jsonObject.put("password", password);
        String data= jsonObject.toString();
       // Log.i("Request", "start http request:" + url);
        try {
            conn = (HttpURLConnection) new URL(url).openConnection();
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setRequestProperty("content-type", "application/json;charset=utf-8");
            //这个writer会把data写到http正文里
            writer = new OutputStreamWriter(conn.getOutputStream());
            writer.write(data);
            writer.flush();
            writer.close();

            //getInputStream开始真正发送
            reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            StringBuilder result = new StringBuilder();
            String line;
            while ((line = reader.readLine()) !=null){
                result.append(line);
            }
            finalResult = result.toString();
        }catch (Exception e){
            e.printStackTrace();
        }
        finally {
            if (conn != null){
                conn.disconnect();
            }
        }
       return finalResult;
    }
    public void postToUIThread(String message){
        final String mes = message;
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), mes, Toast.LENGTH_SHORT).show();
            }
        });
    }
    
}
